package com.hnluchuan.bmjlearning.dao;

import com.hnluchuan.bmjlearning.common.DeleteStatus;
import com.hnluchuan.bmjlearning.dto.CustomerServicePackageDTO;
import com.hnluchuan.bmjlearning.model.CustomerServicePackage;
import com.hnluchuan.bmjlearning.util.CustomDateUtil;
import com.hnluchuan.core.dao.BaseDAO;
import com.hnluchuan.core.support.Page;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Repository
public class CustomerServicePackageDAO extends BaseDAO<CustomerServicePackage> {

    public List<CustomerServicePackage> find(CustomerServicePackageDTO dto, Page page) {
        StringBuilder hql = new StringBuilder();
        List<Object> paras = new ArrayList<Object>();
        hql.append("From CustomerServicePackage n where 1 = 1 ");
        if (dto.getId() != null) {
            hql.append(" and n.id = ? ");
            paras.add(dto.getId());
        }
        if (dto.getUser() != null) {
        	if (dto.getUser().getId() != null) {
	            hql.append(" and n.user.id = ? ");
	            paras.add(dto.getUser().getId());
        	}
        }			
        if (dto.getCustomer() != null) {
        	if (dto.getCustomer().getId() != null) {
	            hql.append(" and n.customer.id = ? ");
	            paras.add(dto.getCustomer().getId());
        	}
        	if (dto.getCustomer().getStatus() != null){
        		hql.append(" and n.customer.status = ? ");
        		paras.add(dto.getCustomer().getStatus());
			}
        }			
        if (dto.getServicePackage() != null) {
        	if (dto.getServicePackage().getId() != null) {
	            hql.append(" and n.servicePackage.id = ? ");
	            paras.add(dto.getServicePackage().getId());
        	}
        }
        if (dto.getServiceType() != null){
        	hql.append(" and n.servicePackage.id = ? ");
        	paras.add(dto.getServiceType());
		}
		if (StringUtils.isNotBlank(dto.getStartDate())){
        	hql.append(" and date_format(n.created, '%Y %m %d') >= date_format(?, '%Y %m %d') ");
        	paras.add(CustomDateUtil.getShortTimeDate(dto.getStartDate()));
		}
		if (StringUtils.isNotBlank(dto.getEndDate())){
			hql.append(" and date_format(n.created, '%Y %m %d') <= date_format(?, '%Y %m %d') ");
			paras.add(CustomDateUtil.getShortTimeDate(dto.getEndDate()));
		}
		if (StringUtils.isNotBlank(dto.getNameOrMobile())){
			hql.append(" and (n.customer.name = ? or n.customer.mobile = ?) ");
			paras.add(dto.getNameOrMobile());
			paras.add(dto.getNameOrMobile());
		}
		if (dto.getDeleted() != null){
			hql.append(" and n.deleted = ? ");
			paras.add(dto.getDeleted());
		}
        hql.append(" order by n.id desc ");
        return super.find(hql.toString(), paras.toArray(), page);
    }
    
    public CustomerServicePackage findOne(CustomerServicePackageDTO dto) {
		Page page = new Page();
		page.setPageSize(1);
		page.setPage(1);
		
		List<CustomerServicePackage> list = this.find(dto, page);
	    return CollectionUtils.isEmpty(list) ? null : list.get(0);
    }

    public List<CustomerServicePackage> findByAgentUserId(Long agentUserId){
    	String hql = "From CustomerServicePackage csp where csp.user.id = ?";
    	List<Object> paras = new ArrayList<>();
    	paras.add(agentUserId);

    	return super.find(hql, paras.toArray(), null);
	}

	public int deleteAllCustomerServicePackageByCustomerId(Long customerId){
		String hql = "Update CustomerServicePackage a set a.deleted = ? where a.customer.id = ?";
		List<Object> paras = new ArrayList<>();
		paras.add(DeleteStatus.Deleted.getValue());
		paras.add(customerId);

		return super.executeUpdate(hql, paras.toArray());
	}

	public List<CustomerServicePackage> findByCustomerId(Long customerId){
		String hql = "From CustomerServicePackage csp where csp.customer.id = ? and csp.deleted = ? order by created asc";
		List<Object> paras = new ArrayList<>();
		paras.add(customerId);
		paras.add(DeleteStatus.NotDeleted.getValue());

		return super.find(hql, paras.toArray(), null);
	}

    @Override
	public Long create(CustomerServicePackage customerServicePackage) {
	    if (customerServicePackage.getCreated() == null) {    
	        customerServicePackage.setCreated(new Date());
	    }    
		return super.create(customerServicePackage);
	}

	@Override
	public void update(CustomerServicePackage customerServicePackage) {
		super.update(customerServicePackage);
	}

}
